package com.example.mp3player;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class Mp3Widget extends AppWidgetProvider {
	
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		// Register an onClickListener
		Intent service = new Intent(context, Mp3Service.class);
		service.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
		context.startService(service);
	}
	
	@Override
	public void onEnabled(Context context){
		super.onEnabled(context);
		setWidgetActive(context, true);
	}
	
	@Override
	public void onDisabled(Context context){
		super.onDisabled(context);
		setWidgetActive(context, false);
	}
	
	private void setWidgetActive(Context context, boolean active){
		SharedPreferences shPref = context.getSharedPreferences("mp3", Context.MODE_WORLD_READABLE);
		Editor edit = shPref.edit();
		edit.putBoolean("widget", active);
		edit.commit();
	}
}
